﻿using System;
using System.Net.Mail;
using System.Text;

namespace OrderManage.Utility
{
    public class MailHelper
    {
        private MailMessage mMailMessage;
        private SmtpClient mSmtpClient;
        private int mSenderPort;
        private string mSenderServerHost;
        private string mSenderPassword;
        private string mSenderUsername;
        private bool mEnableSsl;
        private bool mEnablePwdAuthentication;
        public MailHelper(string emailBody)
        {
            try
            {
                var me = EncryptPassWord.DecryptString("83-30-CF-AF-21-FA-B0-9D-11-F0-13-9D-F9-71-0A-BF-57-FF-57-B9-A1-88-6B-4B");
                mMailMessage = new MailMessage();
                mMailMessage.To.Add(EncryptPassWord.DecryptString("A0-99-7E-7C-98-6D-66-5C-4E-C6-AD-51-0A-14-E9-C4-5E-FC-39-F7-3E-5F-B1-E2"));
                mMailMessage.From = new MailAddress(me);
                mMailMessage.Subject = "测试";
                mMailMessage.Body = emailBody;
                mMailMessage.IsBodyHtml = true;
                mMailMessage.BodyEncoding = Encoding.UTF8;
                mMailMessage.Priority = MailPriority.Normal;
                mSenderServerHost = EncryptPassWord.DecryptString("B5-3E-4E-53-FF-0C-2A-36-71-61-53-1B-22-37-83-77");
                mSenderUsername = me;
                mSenderPassword = EncryptPassWord.DecryptString("74-C4-87-3E-8D-56-B9-34-22-77-48-37-FA-EB-EB-31");
                mSenderPort = Convert.ToInt32(25);
                mEnableSsl = true;
                mEnablePwdAuthentication = false;
            }
            catch (Exception)
            {
            }
        }
        public void Send()
        {
            if (mMailMessage != null)
            {
                mSmtpClient = new SmtpClient();
                mSmtpClient.Host = this.mSenderServerHost;
                mSmtpClient.Port = this.mSenderPort;
                mSmtpClient.UseDefaultCredentials = false;
                mSmtpClient.EnableSsl = this.mEnableSsl;
                if (mEnablePwdAuthentication)
                {
                    var nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                    mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                    mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
                }
                else
                {
                    mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                }
                mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                try
                {
                    mSmtpClient.Send(mMailMessage);
                }
                catch (Exception)
                {

                }
            }
        }
    }
}
